﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sio.Mdm.Entities
{
    /// <summary>
    /// Represent range of selected entities. Either by order they appear by <see cref="Query"/>
    /// or by some other means.
    /// </summary>
    /// <seealso cref="Criteria"/>
    /// <seealso cref="QueryParameter"/>
    [Serializable]
    public class QueryRange
    {
        [NonSerialized]
        public static readonly QueryRange Single = new QueryRange(1);
        [NonSerialized]
        public static readonly QueryRange All = new QueryRange(1, Int32.MaxValue);        

        #region Construction
        public QueryRange()
            : this(1, 1)
        {
        }
        public QueryRange(int count)
            : this(1, count)
        {
        }
        public QueryRange(int start, int count)
        {
            if (start < 1)
            {
                throw new ArgumentException("Start must positive number (and zero is not positive number)");
            }

            Start = start;
            Count = count;
        } 
        #endregion

        #region Start
        private int _start = -1;
        public int Start
        {
            get
            {
                return _start;
            }
            set
            {
                _start = value;
            }
        } 
        #endregion

        #region End
        public int End
        {
            get
            {
                return Start + Count;
            }
        }
        
        #endregion

        #region Count
        private int _count = 1;
        public int Count
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
            }
        } 
        #endregion

        public int TotalCount { get; set; }

        public static QueryRange Top(int count)
        {
            return new QueryRange(count);
        }
        public static QueryRange Range(int start, int end)
        {
            if (end <= start)
            {
                throw new ArgumentException("End must be greeater than start!");
            }

            return new QueryRange(start, end - start + 1);
        }        
        public override string ToString()
        {
            if (Start == -1)
            {
                return String.Format("{0} record(s)", Count);
            }

            return String.Format("From {0} to {1}", Start, Start + Count);
        }
    }
}
